﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SGRapidForm.Common;
using SGRapidForm.Permission.Entity;
using FishWebLib;
using System.Collections.Specialized;
using System.Data;

namespace SGRapidForm.Permission.Component
{
    public class UserDeptComponent : ComponentBase
    {
        /// <summary>
        /// 添加一位新用户
        /// </summary>
        /// <param name="UserDetails">一位用户</param>
        /// <returns>是否添加成功</returns>
        public bool AddUser(UserDetails userDetails)
        {
            return (FishBLLHelper.CallSpExecuteNonQuery("sgrf_permission_User_Add", userDetails) > 0);
        }

        /// <summary>
        /// 删除一位用户
        /// </summary>
        /// <param name="keyid">用户的Keyid</param>
        /// <returns>是否删除成功</returns>
        public bool DeleteUser(string keyid)
        {
            return (FishBLLHelper.CallSpExecuteNonQuery("sgrf_permission_User_Delete", null, keyid) > 0);
        }


        public bool UserDept_Add(UserDept userDept)
        {
            return FishBLLHelper.CallSpExecuteNonQuery("sgrf_permission_UserDept_Add", userDept) > 0;
        }

        public bool UserDept_BulkAdd(List<UserDept> UserDepts)
        {
            foreach (UserDept item in UserDepts)
            {
                FishBLLHelper.CallSpExecuteNonQuery("sgrf_permission_UserDept_Add", item);
            }
            return true;
        }

        public bool UserDept_Delete(string userKeyid, string deptKeyid)
        {
            return FishBLLHelper.CallSpExecuteNonQuery("sgrf_permission_UserDept_Delete", null, userKeyid, deptKeyid) > 0;
        }

        /// <summary>
        /// 为用户批量移除角色
        /// </summary>
        /// <param name="userKeyid">用户Keyid</param>
        /// <param name="roleKeyid">角色Keyid集合</param>
        /// <returns></returns>
        public bool UserDept_BulkDelete(string userKeyid, string[] deptKeyids)
        {
            for (int i = 0; i < deptKeyids.Length; i++)
            {
                this.UserDept_Delete(userKeyid, deptKeyids[i]);
            }
            return true;
        }

        public bool UserDept_Exists(string userKeyid, string deptKeyid)
        {
            return (int)FishBLLHelper.CallSpExecuteScalar("sgrf_permission_UserDept_Exists", null, userKeyid, deptKeyid) > 0;
        }

        public List<Dept> UserDept_GetDeptByUserKeyid(string userKeyid)
        {
            return FishBLLHelper.CallSpGetDataItemList<Dept>("sgrf_permission_UserDept_GetDeptByUserKeyid", null, userKeyid);   
        }

        public List<Dept> UserDept_GetUnmappedDeptByUserKeyid(string userKeyid, string filterConditions)
        {
            NameValueCollection fieldCollection = SqlHelper.ParseQueryString(filterConditions);
            StringBuilder sql = new StringBuilder();
            sql.Append(SqlHelper.GenerateSqlStatement(fieldCollection, "DeptName", DbType.String, " AND DeptName like '%{0}%' "));
            sql.Append(SqlHelper.GenerateSqlStatement(fieldCollection, "DeptCode", DbType.String, " AND DeptCode like '%{0}%' "));
            sql.Append(SqlHelper.GenerateSqlStatement(fieldCollection, "HelpCode", DbType.String, " AND HelpCode like '%{0}%' "));
            sql.Append(SqlHelper.GenerateSqlStatement(fieldCollection, "DeptType", DbType.Int32, " AND DeptType = '{0}' "));
            filterConditions = sql.ToString();

            return FishBLLHelper.CallSpGetDataItemList<Dept>("sgrf_permission_UserDept_GetUnmappedDeptByUserKeyid", null, userKeyid, filterConditions);
        }
    }
}
